﻿
namespace UsingNonMVVMElements
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    
    /// <summary>
    /// Interaction logic for WebBrowserAdapter.xaml
    /// </summary>
    public partial class WebBrowserAdapter 
        : UserControl, IWebBrowserAdapter
    {
        public WebBrowserAdapter()
        {
            InitializeComponent();
        }
        
        #region Dependency Properties

        public static DependencyProperty SourceProperty = 
            DependencyProperty.Register(
            "Source", typeof(string), typeof(WebBrowserAdapter), 
            new PropertyMetadata(string.Empty));

        public string Source
        {
            get { return (string)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }
                
        #endregion

        #region Overriden methods

        protected override void OnPropertyChanged(
            DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if (e.Property == SourceProperty &&
                e.NewValue != null &&
                !string.Equals(webBrowser.Source, e.NewValue))
            {
                webBrowser.Source = 
                    new Uri(e.NewValue.ToString());
            }
        }

        #endregion

    }
}
